/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.teamtools.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.teamtools.domain.IProject;
import com.google.code.teamtools.domain.ITeam;
import com.google.code.teamtools.domain.impl.Employee;
import com.google.code.teamtools.persist.IEmployeeDao;
import com.google.code.teamtools.service.IEmployeeService;

/**
 * Employee service implementation.
 * 
 * @author David Kuhn
 */
public class EmployeeService implements IEmployeeService
{
	private static final Logger LOG = LoggerFactory.getLogger(EmployeeService.class);
	
	private IEmployeeDao employeeDao;

    /**
     * Injection method for the <code>IEmployee</code> persistence mechanism.
     * 
     * @param employeeDao the <code>IEmployeeDao</code> instance to set
     */
    public void setEmployeeDao(IEmployeeDao employeeDao)
    {
        this.employeeDao = employeeDao;
    }
	
	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#delete(com.google.code.teamtools.domain.impl.Employee)
	 */
	public void delete(Employee employee)
	{
		if (LOG.isDebugEnabled()) LOG.debug("Deleting " + employee.toString() + "...");
		employeeDao.delete(employee);
		LOG.info("Deleted " + employee.toString() + " successfully.");
	}
	
	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#deleteById(java.lang.Long)
	 */
	public void deleteById(Long id)
	{
	    if (LOG.isDebugEnabled()) LOG.debug("Deleting employee with ID: [" + id + "]...");
	    delete(employeeDao.findById(id));
	    if (LOG.isDebugEnabled()) LOG.debug("Employee with ID: [" + id + "] removed successfully.");
	}
	
	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#getEmployeeById(java.lang.Long)
	 */
	public Employee getEmployeeById(Long id)
	{
	    if (LOG.isDebugEnabled()) LOG.debug("Fetching employee with ID: [" + id + "]...");
	    return employeeDao.findById(id);
	}
	
	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#getPeople()
	 */
	public List<Employee> getEmployees()
	{
		if (LOG.isDebugEnabled()) LOG.debug("Retrieving all employees in system...");
		return employeeDao.retrieveAll();
	}

	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#getEmployeesByProject(com.google.code.teamtools.domain.IProject)
	 */
	public List<Employee> getEmployeesByProject(IProject project)
	{
	    return null;
	}
	
	/* (non-Javadoc)
	 * @see com.google.code.teamtools.service.IEmployeeService#getPeople(com.google.code.teamtools.domain.IResourceOwner)
	 */
	public List<Employee> getEmployeesByTeam(ITeam team)
	{
		if (LOG.isDebugEnabled()) LOG.debug("Retrieving all people for team [" + team + "]...");
		Employee p = new Employee(null, team);
		return employeeDao.findByExample(p, "name");
	}
	
	/* (non-Javadoc)
     * @see com.google.code.teamtools.service.IEmployeeService#createOrUpdate(com.google.code.teamtools.domain.impl.Employee)
     */
    public void saveOrUpdate(Employee employee)
    {
        if (LOG.isDebugEnabled()) LOG.debug("Saving " + employee.toString() + "...");
        employeeDao.save(employee);
        LOG.info("Saved " + employee.toString() + " successfully.");
    }
}
